#!/usr/bin/env python
# encoding: utf-8
# Author: Zgames <zgames(at)yeah(dot)net>
# Waf 构建文件
# 使用方法：在命令行输入
# python waf configure build install

import os, string, os.path

top = "."
out = "waf-build"


def options(ctx):
    ctx.load("gcc gxx winres")


def configure(ctx):
    ctx.load("gcc gxx winres")


def build(ctx):
    src_files = """
        jyres.rc
        mainmap.cpp
        script.cpp
        main.cpp
        charset.cpp
        util.cpp
        lodepng.cpp
        image.cpp
        font.cpp
    """

    sdkpath = ctx.env.SDK_PATH
    library_paths = [
        sdkpath + "/glib-2.0/lib",
        sdkpath + "/lua/lib",
        ctx.env.SDL_LIB_PATH,
    ]

    proot = ctx.env.PROJECT_ROOT
    include_paths = [
        sdkpath + "/lua/include",
        proot + "/source/libzaudio",
        proot + "/source/libfluidsynth/include",
        ctx.env.SDL_INCLUDE_PATH,
    ]
    defines = [
        "HAVE_VSNPRINTF",
    ]
    install_path = ctx.env.INSTALL_PATH
    shared_libs = "wsock32 glib-2.0.dll gthread-2.0.dll"
    static_libs = "mingw32 lua"

    if ctx.env.SDL_VERSION == "1.2":
        src_files += """
            audio_null.cpp
            video_sdl.cpp
            input_sdl.cpp
        """
        shared_libs += """
            SDLmain
            SDL
        """
    elif ctx.env.SDL_VERSION == "2.0":
        src_files += """
            audio_sdl2.cpp
            video_sdl2.cpp
            input_sdl2.cpp
        """
        shared_libs += """
            SDL2main
            SDL2
        """
    ctx.recurse(["libzaudio"])
    ctx(
        rule = "copy ${SRC} ${TGT}",
        source = "script/main.lua",
        target = "main.lua",
        install_path = install_path + "/script"
    )
    ctx.program(
        source = src_files.split(),
        target = "legend", 
        libpath = library_paths,
        linkflags = "-mwindows",
        cxxflags = "-O2",
        defines = defines,
        lib = shared_libs,
        stlib = static_libs,
        includes = include_paths,
        install_path = install_path,
        use = ["zaudio"]
    )
